package com.itheima.store.web.servlet;

import java.sql.Date;
import java.sql.SQLException;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.itheima.store.domain.SystemSetting;
import com.itheima.store.service.SystemSettingService;
import com.itheima.store.utils.BaseServlet;
import com.itheima.store.utils.BeanFactory;
import com.itheima.store.utils.UUIDUtils;

/**
 * Servlet implementation class AdminSystemServlet
 */
public class AdminSystemServlet extends BaseServlet {
	//左侧菜单显示list.jsp
	public String showSystemListUI(HttpServletRequest req,HttpServletResponse resp){
		SystemSettingService systemSettingService = (SystemSettingService)BeanFactory.getBean("systemSettingService");
		try {
			SystemSetting systemSetting = systemSettingService.findSystemSetting();
			req.setAttribute("systemSetting", systemSetting);
			return "/admin/systemSetting/list.jsp";
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	
	//保存系统设置
	public String saveSystemSetting(HttpServletRequest req,HttpServletResponse resp){
		//1.获取参数
		Map<String, String[]> map = req.getParameterMap();
		SystemSetting systemSetting = new SystemSetting();
		try{
			//2.封装到JavaBean systemSetting 
			org.apache.commons.beanutils.BeanUtils.populate(systemSetting, map);
			String sys_setting_id = UUIDUtils.getUUID();
			systemSetting.setSys_setting_id(sys_setting_id);
			SystemSettingService systemSettingService = (SystemSettingService)BeanFactory.getBean("systemSettingService");
			systemSettingService.saveSystemSetting(systemSetting);
			req.setAttribute("systemSetting", systemSetting);
			return "/admin/systemSetting/list.jsp";
		}catch(Exception e ){
			
		}
		return null;
	}
	
}
